#region

using System;
using EditeurSyntaxiqueVocal.Class;

#endregion

namespace EditeurSyntaxiqueVocal.Class
{
    ///<summary>
    ///</summary>
    public static class ConverterEntreStringNombre
    {
        /// <summary>
        /// ones.
        /// </summary>
        private static readonly string[] ones = new string[]
                                                    {
                                                        "one",
                                                        "two",
                                                        "three",
                                                        "four",
                                                        "five",
                                                        "six",
                                                        "seven",
                                                        "eight",
                                                        "nine",
                                                        "ten"
                                                    };

        /// <summary>
        /// teens.
        /// </summary>
        private static readonly string[] teens = new string[]
                                                     {
                                                         "ten",
                                                         "eleven",
                                                         "twelve",
                                                         "thirteen",
                                                         "fourteen",
                                                         "fifteen",
                                                         "sixteen",
                                                         "seventeen",
                                                         "eighteen",
                                                         "nineteen"
                                                     };

        /// <summary>
        /// tensfrag.
        /// </summary>
        private static readonly string[] tensfrag = new string[]
                                                        {
                                                            "twenty",
                                                            "thirty",
                                                            "fourty",
                                                            "fifty",
                                                            "sixty",
                                                            "seventy",
                                                            "eighty",
                                                            "ninety"
                                                        };

        /// <summary>
        /// 
        /// </summary>
        private static readonly string[,] up =
            {
                {
                    "trillion",
                    "trillions"
                }
                ,
                {
                    "billion",
                    "billions"
                }
                ,
                {
                    "million",
                    "millions"
                }
                ,
                {
                    "thousand",
                    "thousands"
                }
                ,
                {
                    "hundred",
                    "hundreds"
                }
            };

        private static bool _ConverterVirefier = true;

        /// <summary>
        /// 
        /// </summary>
        public static bool ConverterVirefier
        {
            get { return _ConverterVirefier; }
        }

        #region Entier Vers String

        /// <summary>
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static string ConverterNumeroAString(int number)
        {
            switch (number)
            {
                case 0:
                    return "zero";

                default:
                    return ConverterNumeroHundredAString(number);
            }
        }


        public static string ConverterNumeroHundredDownAString(int i)
        {
            if (i <= 10)
                return ones[i - 1];
            if (i < 20)
                return teens[i - 10];
            int a = i % 10;
            int b = i / 10;
            return (b == 0 ? "" : tensfrag[b - 2]) + " " + (a == 0 ? "" : ones[a - 1]);
        }

        public static string ConverterNumeroHundredAString(int i)
        {
            if (i > 99)
            {
                int a = i % 100;
                int b = i / 100;
                return ones[b - 1] + " hundred " + ConverterNumeroHundredDownAString(a);
            }
            return ConverterNumeroHundredDownAString(i);            
        }

        #endregion


        #region String Vers Entier

        /// <summary>
        /// 
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        public static Int64 ConverterStringANumero(string word)
        {
            word = word.Replace("and", "").Trim().ToLower();
            if(word.Trim()=="-"||word.Trim()=="+")
            {
                _ConverterVirefier = false;
                return 0;
            }
            string ch = word.StartsWith("-") ? "-1" : "+1";
            word = word.Replace("-", "").Replace("+", "").Trim().TrimEnd();
            switch (word)
            {
                case "zero":
                    return 0;
                default:
                    return int.Parse(ch) * ConverterStringTrillionANumero(word);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static int ConverterStringHundredDownANumero(string str)
        {
            string s = str = str.Trim().TrimEnd();

            int nb = -1;
            for (int i = 0; i < teens.Length; i++)
            {
                if (str.IndexOf(teens[i]) < 0)
                    continue;
                nb = i + 10;
                str = str.Replace(teens[i], "");
                break;
            }

            for (int i = 0; i < ones.Length; i++)
            {
                if (str.IndexOf(ones[i]) < 0)
                    continue;
                if (nb == -1)
                    nb = i + 1;
                else nb += i + 1;
                str = str.Replace(ones[i], "");
                break;
            }

            for (int i = 0; i < tensfrag.Length; i++)
            {
                if (str.IndexOf(tensfrag[i]) < 0)
                    continue;
                if (nb == -1)
                    nb = (i + 2) * 10;
                else nb += (i + 2) * 10;
                break;
            }

            if (s == "") nb = 0;
            if (nb != -1)
            {
                _ConverterVirefier = true;
                return nb;
            }
            _ConverterVirefier = false;
            return 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static int ConverterStringHundredANumero(string str)
        {
            str = str.Replace("hundreds", "hundred").Trim().TrimEnd();
            if (str.IndexOf("hundred") < 0) return ConverterStringHundredDownANumero(str);
            int nb;
            return (nb = ConverterStringHundredDownANumero(str.Substring(0, str.IndexOf("hundred")))) == 0 ? 100 : nb * 100
                   + ConverterStringHundredDownANumero(str.Substring(str.IndexOf("hundred") + "hundred".Length));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static Int64 ConverterStringThousandANumero(string str)
        {
            str = str.Replace("thousands", "thousand").Trim().TrimEnd();
            if (str.IndexOf("thousand") < 0) return ConverterStringHundredANumero(str);
            Int64 nb;
            return (nb = ConverterStringHundredANumero(str.Substring(0, str.IndexOf("thousand")))) == 0 ? 1000 : nb * 1000
                   + ConverterStringHundredANumero(str.Substring(str.IndexOf("thousand") + "thousand".Length));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static Int64 ConverterStringMillionANumero(string str)
        {
            str = str.Replace("millions", "million").Trim().TrimEnd();
            if (str.IndexOf("million") < 0) return ConverterStringThousandANumero(str);
            Int64 nb;
            return (nb = ConverterStringThousandANumero(str.Substring(0, str.IndexOf("million")))) == 0 ? 100000 : nb * 100000
                   + ConverterStringThousandANumero(str.Substring(str.IndexOf("million") + "million".Length));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static Int64 ConverterStringBillionANumero(string str)
        {
            str = str.Replace("billions", "billion").Trim().TrimEnd();
            if (str.IndexOf("billion") < 0) return ConverterStringMillionANumero(str);
            Int64 nb;
            return (nb = ConverterStringMillionANumero(str.Substring(0, str.IndexOf("billion")))) == 0 ? 100000000 : nb * 100000000
                   + ConverterStringMillionANumero(str.Substring(str.IndexOf("billion") + "billion".Length));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static Int64 ConverterStringTrillionANumero(string str)
        {
            str = str.Replace("trillions", "trillion").Trim().TrimEnd();
            if (str.IndexOf("trillion") < 0) return ConverterStringBillionANumero(str);
            Int64 nb;
            return (nb = ConverterStringBillionANumero(str.Substring(0, str.IndexOf("trillion")))) == 0 ? 100000000000 : nb * 100000000000
                   + ConverterStringBillionANumero(str.Substring(str.IndexOf("trillion") + "trillion".Length));
        }

        #endregion
    }
}